package com.wzk.canalsync.config;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.net.ssl.SSLContext;

/**
 * @Author: WangZiKang
 * @Date: 2024/12/29 11:52
 */

//es配置
@Configuration
@ConfigurationProperties(prefix = "es")
@Setter
@Getter
public class ElasticSearchConfig {
    //配置项的值
    private String host;
    private Integer port;
    private String username;
    private String password;
    private String requestMethod;
    private Integer connectionTimeOut;
    private Integer readTimeOut;

    @Bean
    public RestHighLevelClient restHighLevelClient(){
        //构建参数
        RestClientBuilder builder = RestClient.builder(
                new HttpHost(host, port, requestMethod)
        );
        // 合并认证和 SSL 配置
        builder.setHttpClientConfigCallback(httpClientBuilder -> {
            // 设置账号密码
            if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
                CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                credentialsProvider.setCredentials(
                        org.apache.http.auth.AuthScope.ANY,
                        new UsernamePasswordCredentials(username, password)
                );
                httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            }
            // 禁用 SSL 证书验证
            try {
                // 创建信任所有证书的 SSLContext
                SSLContext sslContext = SSLContextBuilder.create()
                        .loadTrustMaterial((chain, authType) -> true) // 信任所有证书
                        .build();
                // 设置禁用主机名验证
                httpClientBuilder.setSSLContext(sslContext)
                        .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); // 禁用主机名验证
            } catch (Exception e) {
                e.printStackTrace();
            }

            // 创建 RequestConfig
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(connectionTimeOut)  // 设置连接超时
                    .setSocketTimeout(readTimeOut)    // 设置读取超时
                    .build();
            // 将 RequestConfig 设置到 HttpClient 中
            httpClientBuilder.setDefaultRequestConfig(requestConfig);
            return httpClientBuilder;
        });

        //构建client
        RestHighLevelClient client = new RestHighLevelClient(builder);
        //返回
        return client;
    }
}
